<template>
  <div class="detail-view" v-if="movie">
    <el-row :gutter="20">
      <el-col :span="6">
        <img :src="movie.poster_image" class="detail-poster" />
      </el-col>
      <el-col :span="18">
        <h1>{{ movie.title }}</h1>
        <p><strong>导演:</strong> {{ movie.director }}</p>
        <p><strong>主演:</strong> {{ movie.actors }}</p>
        <p><strong>上映日期:</strong> {{ movie.release_date }}</p>
        <p><strong>片长:</strong> {{ movie.duration_mins }} 分钟</p>
        <h3>简介</h3>
        <p>{{ movie.description }}</p>
      </el-col>
    </el-row>

    <el-divider />
    <h2>评论区</h2>

    <div v-if="userStore.isLoggedIn" class="review-post">
      <h3>发表你的看法</h3>
      <el-form @submit.prevent="postReview">
        <el-form-item label="评分">
          <el-rate v-model="newReview.rating" :max="5" show-text />
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="newReview.content" rows="3" />
        </el-form-item>
        <el-button type="primary" native-type="submit">提交评论</el-button>
      </el-form>
    </div>
     <div v-else>
      <p><router-link to="/login">登录</router-link> 后才能发表评论</p>
    </div>

    <div class="review-list">
      <el-card v-for="review in movie.reviews" :key="review.id" class="review-card">
        <strong>{{ review.username }}</strong>
        <el-rate :model-value="review.rating" disabled />
        <p>{{ review.content }}</p>
        <small>{{ new Date(review.created_at).toLocaleString() }}</small>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '../utils/request'
import { useUserStore } from '../store/user'

const route = useRoute()
const userStore = useUserStore()
const movie = ref(null)
const movieId = route.params.id // P5: 获取动态路由参数 :id (满足 3.7)

const newReview = ref({
  rating: 0,
  content: ''
})

// P5: 封装获取电影详情的函数 (P5 调用 P2 的 API)
const fetchMovieDetail = async () => {
  try {
    const res = await request.get(`/movie/${movieId}`)
    if (res.data.success) {
      movie.value = res.data.data
    }
  } catch (error) {
    console.error('获取详情失败:', error)
  }
}

// P5: 提交评论 (P5 调用 P3 的 API)
const postReview = async () => {
  try {
    const res = await request.post('/review', {
      movie_id: movieId,
      rating: newReview.value.rating,
      content: newReview.value.content
    })
    if (res.data.success) {
      ElMessage.success('评论成功')
      newReview.value.rating = 0
      newReview.value.content = ''
      await fetchMovieDetail() // P5: 评论成功后, 立即刷新评论列表
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '评论失败')
  }
}

// P5: 页面加载时执行
onMounted(() => {
  fetchMovieDetail()
})
</script>

<style scoped>
.detail-view { max-width: 1000px; margin: 0 auto; }
.detail-poster { width: 100%; object-fit: cover; }
.review-post { margin-bottom: 20px; }
.review-list { margin-top: 20px; }
.review-card { margin-bottom: 10px; }
</style>